Skip to content

Make MP4 decoding on Windows start and end where Core Audio does - #83

Open
ni-eeviston wants to merge 9 commits into
NativeInstruments:masterfrom
ni-eeviston:bugfix/mp4-encoder-delay
Open

ni-eeviston wants to merge 9 commits into
NativeInstruments:masterfrom
ni-eeviston:bugfix/mp4-encoder-delay

Conversation

@ni-eeviston

@ni-eeviston ni-eeviston commented Nov 27, 2026 •

Copy link
Copy Markdown

On Windows, MP4/M4A audio decoded through Media Foundation started and ended at different samples than on macOS (Core Audio), and its first milliseconds were faded in. ALAC files lost their first 2112 frames, and seeks near the end of ALAC files could return silence. This makes the Windows decoder match Core Audio.

Each commit is a self-contained change so reviewing by commit may be easiest:

  1. Stop fading in the start of MP4 audio on Windows
  2. Seek further back when a discarded block reaches the target on Windows
    (fixes seeks near the end of ALAC files returning silence)
  3. Skip the encoder priming of MP4 files as Core Audio does on Windows
    (the edit list, iTunSMPB tag or standard 2112 frames, chosen as Core Audio
    chooses them)
  4. Run the MP4 tests on Windows, previously they ran only on macOS.

Impact

  • Windows only; macOS/iOS/Linux decoding is unchanged. Only MP4/M4A is affected.
  • Decoded M4A output changes on Windows: audio now starts and ends at the same
    sample as on macOS (shifts of up to 2112 frames, ~48 ms at 44.1 kHz) and no
    longer fades in. Sample positions stored for M4A files analysed on Windows,
    such as Traktor cue points and beat grids, may shift; they will now match
    macOS. Are there other products using this path whose owners should know?

Testing

  • Windows: full test suite passes (2979 tests), including the new priming tests,
    whose expected results were measured with Core Audio on macOS.
  • Checked against 105 files measured with Core Audio: Apple encoder output
    (afconvert, avconvert, AVAssetWriter; AAC-LC, HE-AAC, ALAC; .m4a and .mp4) and
    a hand-made matrix of brand x roll group x edit list x iTunSMPB combinations.
    95 decode identically on Windows; the rest are listed below.

Known differences from Core Audio (not addressed here)

  • HE-AAC: Media Foundation decodes at the core sample rate (half the output
    rate), so length and alignment differ. This already happened before this PR.
  • An empty edit before the first edit-list entry: Core Audio ignores it; on
    Windows the start is off by 1024 frames. Rare in audio files.
  • An edit list starting past the end of the media (malformed): Core Audio
    decodes nothing; this PR ignores the edit list and decodes the file.

Context: MASSIVEX-4484, where M4A wavetable imports started at a different point on Windows than on macOS. Its unit tests pass with this change.

  AAC files begin with a short stretch of encoder "priming" that decoders
  must skip. When opening an MP4 file, media_foundation_source jumped past
  it to the start of the real audio. But Windows Media Foundation fades in
  the first audio it decodes after any jump, so the first milliseconds of
  every AAC file came out faded. Seeking near the start had the same
  effect.

  Decode from the beginning of the file instead. Any fade-in now falls on
  the priming, which is thrown away.

  Also fix seekInternal remembering the first file's priming length for
  the rest of the session (it was stored in a static variable). This was
  harmless while every MP4 file was treated as having the same priming
  length, but would break once it is read from each file.
@ni-eeviston
ni-eeviston force-pushed the bugfix/mp4-encoder-delay branch 4 times, most recently from fbd4ea7 to 519448b Compare November 27, 2026 17:31
@ni-eeviston
ni-eeviston marked this pull request as ready for review November 27, 2026 17:31
@ni-eeviston

Copy link
Copy Markdown
Author

@marcrambo @FalconPDX @ni-mheppner can you add review this please? i can't seem to add reviewers.

i will send the link for this PR to my Synths & FX team as well.
someone from Traktor should take a look too.

@ni-eeviston
ni-eeviston force-pushed the bugfix/mp4-encoder-delay branch from 519448b to 329c1d2 Compare November 30, 2026 14:02
 After a seek, media_foundation_source discards the first two decoded
 blocks, because Media Foundation fades them in. It jumps two blocks of
 1024 frames before the target to make room for them. ALAC decodes 4096
 frames per block and MP3 about 15000, so a discarded block could
 already contain the target. Near the end of the file nothing followed
 it, and the seek returned silence.

 Jump back two of the largest blocks decoded so far instead. When a
 discarded block still reaches the target, seek further back and try
 again, as is already done when a seek lands past the target, doubling
 the step each time so a seek takes only a few tries.
  AAC files begin with a short stretch of encoder "priming" that decoders
  must skip, and often end with some "padding". media_foundation_source
  skipped a fixed 2112 frames at the start of every MP4 file, the priming
  of Apple's AAC encoder, and kept the padding. Files with any other
  priming started at a different sample than on macOS, and ALAC files,
  which have no priming, lost their first 2112 frames.

  Files declare their priming in an iTunSMPB tag, as iTunes writes, or
  in the edit list, as ffmpeg and Apple's MP4 writer do. Measured with
  Core Audio on macOS, the declaration it follows is:
  - the edit list, if the sample table has a roll sample group, as newer
    encoders write;
  - otherwise the iTunSMPB tag, if 'M4A ' or 'M4B ' appears among the
    file's brands;
  - otherwise, for AAC, the standard 2112 frames, with nothing trimmed
    from the end, which counts the last packet in full.
  Do the same. Media Foundation reports the iTunSMPB values, but the
  edit list, roll sample group, brands and packet counts are read from
  the file.

  Media Foundation takes its duration from the movie header, which
  excludes the priming and padding when the file has an edit list. So
  the length is taken from the media header when the iTunSMPB tag
  decides, and from the packet count when the standard priming is
  assumed.

  Media Foundation on Windows 10 timestamps the first decoded frame
  with the start of the edit list, while Windows 11 timestamps it with
  0; both decode the same frames. The timestamp of the first frame is
  read when the file is opened and taken as the origin of all
  timestamps and seek positions.

  The file's sound tracks are only used when they match the audio
  streams Media Foundation reports. Otherwise, AAC without an iTunSMPB
  tag keeps the standard 2112 frames. An edit list that starts past the
  end of the media is malformed and ignored, and parsing stops at a box
  that doesn't fit in the file.

  Test files, each a 1 second 440 Hz sine:
  - sin440.1.44100.44100.s16le.aac192.ffmpeg.m4a, a reference file whose
    priming of 1024 frames is declared by an edit list, made with ffmpeg
    (libavformat 63.21.100):
      ffmpeg -f lavfi -i "sine=frequency=440:sample_rate=44100:duration=1" \
             -ac 1 -c:a aac -b:a 192k sin440.1.44100.44100.s16le.aac192.ffmpeg.m4a
  - sin440.ffmpeg.*.m4a: that file with its edit list set to play all of
    the media, its roll sample group removed, or both;
  - sin440.apple.*.m4a: a file encoded by afconvert, whose encoder primed
    2112 frames, with its edit list, iTunSMPB tag, roll sample group and
    brands changed to each case the rule distinguishes.
  The expected results were measured with Core Audio on macOS.
  The MP4 decoding tests only ran on macOS, so Windows MP4 decoding was
  never tested. Run them on Windows too, and add a test that seeking
  returns the same audio as reading from the start, as
  media_foundation_source discards the encoder priming and the blocks
  Media Foundation fades in after a seek.
@ni-eeviston

Copy link
Copy Markdown
Author

CI: Windows 2022 and Ubuntu 22.04 pass. The macOS, Ubuntu 20.04 and Code-Coverage jobs never start on any PR because they target paid or retired runners. macOS was tested locally on Apple Silicon (M1) and in a fork CI run on macos-15-intel. I think in order to make them run, ci.yml needs the macOS jobs on the standard macos-15 runners and the Ubuntu 20.04 and Code-Coverage jobs on ubuntu-24.04?

@FalconPDX

Copy link
Copy Markdown
Collaborator

would be great to update the runner images - here's a list of what they have available: https://github.com/actions/runner-images

@ni-mheppner

Copy link
Copy Markdown
Contributor

would be great to update the runner images - here's a list of what they have available: https://github.com/actions/runner-images

Thanks for the pointer. I can do that some time this week. @ni-eeviston Is it ok for you, if I just add the change on top of your branch, or would prefer to have this separately integrated before your work lands?

@ni-eeviston

Copy link
Copy Markdown
Author

would be great to update the runner images - here's a list of what they have available: https://github.com/actions/runner-images

Thanks for the pointer. I can do that some time this week. @ni-eeviston Is it ok for you, if I just add the change on top of your branch, or would prefer to have this separately integrated before your work lands?

sure, do whatever is easiest for you

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants